﻿using System.Collections.Generic;
using System.Linq;
using ClientOverflow.DataTransferObjects;

namespace ClientOverflow.CommunicationServices.Data
{
    /// <summary>
    /// UsersFactory is resposible for generating users as objects from DB. In addition, it's
    /// resposible for generating user as object based on crieteria.
    /// </summary>
    public static class UsersFactory
    {
        public static DataObjects.User CreateUser(User dtoUser)
        {
            return dtoUser.ToDataObject();
        }

        public static DataObjects.User GetUser(User dtoUser)
        {
            return DataObjects.User.Queryable.First(user => user.Email == dtoUser.Email);
        }

        public static User GetUser(string email, string password)
        {
            var matchingDbUser = DataObjects.User.Queryable
                .FirstOrDefault(user => email == user.Email && password == user.Password );

            if(matchingDbUser == null)
            {
                return null;
            }

            return matchingDbUser.ToDto();
        }

        public static List<User> GetUsers()
        {
            return DataObjects.User.Queryable.ToList().Select(user => user.ToDto()).ToList();
        }

    }
}